﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

using hes.models;
using hes.data_access;

namespace hes.controllers
{

    // MaintainAssignmentsController handles Adding/Deleting/Updating Course Information
    class MaintainAssignmentsController
    {

        // Function: AddAssignment
        // Adds an assignment to the db having <stuid>, <name>, <category>, <points_earned>, and <points_possble> as the data to go in the db
        // Preconditions: assumes that the above parameters passed from the UI is valid
        // Postconditions: the database is updated and an assignment is returned
        public Assignment AddAssignment(int stuid, string name, int category, int points_earned = 0, int points_possible = 0)
        {

            try
            {
                Assignment new_assmt = new Assignment(name, category, points_possible, points_earned);
                AssignmentDAO assmt_dao = new AssignmentDAO(Connector.Instance.Connection);
                int asmmt_id = assmt_dao.AddAssignment(new_assmt);
                new_assmt.AssignmentID = asmmt_id;
                Debug.WriteLine(DateTime.Now + @": User has succesfully added an Assignment called " + new_assmt.Title + @". - MaintainAssignmentsController");
                Debug.WriteLine("Here are the details: " + new_assmt.Title + " is now the Title. " + new_assmt.PointsEarned + " is now the number of points earned. " +
                    new_assmt.PointsPossible + " is the number of points possible. " + new_assmt.CategoryID + " is now the assignment category id.");

                return new_assmt;
            }
            catch(Exception e) {
                e.ToString();
                Debug.WriteLine(DateTime.Now + ": Database error occurred while in MaintainAssignmentsController in the AddAssignment method");
                return null;
            }
        }

        // Function: DeleteAssignment
        // Deletes an assignment from the database having <stuid> and <name> and returns the assignment deleted
        // Preconditions: assumes that the above parameters passed from the UI is valid
        // Postconditions: the database has lost one assignment and the deleted assignment is returned
        public void DeleteAssignment(int asmt_id)//Assignment assmt)
        {
            try
            {
                AssignmentDAO assmt_dao = new AssignmentDAO(Connector.Instance.Connection);
                assmt_dao.DeleteAssignment(asmt_id);
                Debug.WriteLine(DateTime.Now + ": User has succesfully deleted an Assignment with Assignment ID of " + asmt_id + " - MaintainAssignmentsController");
            }
            catch (Exception e)
            {
                e.ToString();
                Debug.WriteLine(DateTime.Now + ": Database error occurred while in MaintainAssignmentsController in the DeleteAssignment method");

            }
        }

        // Function: UpdateAssignment
        // Handles updating the assignment <name> of <stuid> by either changing any of the fields.
        // Preconditions: assumes that the above parameters passed from the UI is valid
        // Postconditions: Updates an assignment in the db with new information, and returns it.
        public void UpdateAssignment(int old_asmt_id, Assignment new_asmt)
        {
            try
            {
                AssignmentDAO assmt_dao = new AssignmentDAO(Connector.Instance.Connection);
                assmt_dao.UpdateAssignment(old_asmt_id, new_asmt);
                Debug.WriteLine(DateTime.Now + ": User has succesfully updated an Assignment with Assignment ID of " + old_asmt_id + " - MaintainAssignmentsController");
                Debug.WriteLine("Changes include: " + new_asmt.Title + " is now the Title. " + new_asmt.PointsEarned + " is now the number of points earned. " +
                    new_asmt.PointsPossible + " is the number of points possible. " + new_asmt.CategoryID + " is now the assignment category id.");
            }
            catch (Exception e)
            {
                e.ToString();
                Debug.WriteLine(DateTime.Now + ": Database error occurred while in MaintainAssignmentsController in the UpdateAssignment method");
            }
            
        }
    }
}
